import React , { PureComponent } from "react";
import { withRouter } from "react-router";

class CacheComponent extends PureComponent {
    constructor(...agr){
        super()
    }
    static defaultProp = {
        routeData:{} ,
        freshData:{
            type:Function , 
            default:()=>{}
        },
        needCache:{
            type : Boolean ,
            default :false
        },
    }
    componentDidMount(){
        this.props.freshData() ;
        console.log(this.props , '2222222')
    }
    componentWillUnmount(){
        if(this.props.needCache){
            this.loadComponent()
        }
    }
    loadComponent(){
        let { history , location } = this.props ;
        let sessionKey = location.pathname ;
        //该状态为点击返回时。
        window.sessionStorage.setItem(sessionKey,JSON.stringify({
            scroll: document.documentElement.scrollTop
        }))
    }
    render(){
        return <div>{this.props.children}</div>
    }

}
export default withRouter(CacheComponent)